import React from 'react';
import {Table, Row, Col, Form, Input, Radio, Select, Button, Tabs, message} from 'antd';
import BizParams from '../../../components/commons/BizParams'
import OrderDetail from '../../../components/order/components/OrderDetail';
const R = require('ramda');
import Constants from "../../../utils/Constants";
const FormItem = Form.Item;
const RadioGroup = Radio.Group;


export default Form.create({wrappedComponentRef: true})(class Order extends React.Component {
    constructor(props) {
        super(props);
    }

    handleResetFields = () => {
        this.props.form.resetFields();
    }


    onView = (record) => {
        this.handleOpenOrderDetail(record, true, true, true, true, {}, "1");
    }


    handleOpenOrderDetail = (record, orderDisabled, letterDisabled, costDisabled, attachmentDisabled, searchConditions, tabActiveKey) => {
        this.props.dispatch({ type: 'orderManage/getOrderById', payload: { id: record.id } });//获取订单详情
        this.props.dispatch({ type: 'orderManage/queryOrderCost', payload: { orderId: record.id, status: "2", costPage: this.props.orderManage.costPage } });//查询费用列表
        this.props.dispatch({ type: 'orderManage/queryLinkmanList', payload: { cstId: record.providerId } });//查询联系人列表
        this.props.dispatch({ type: 'customs/getListForPage', payload: { page: Constants.DEFAULT_PAGE } }); //查询海关信息

        // this.props.dispatch({ type: 'orderManage/queryAddressList', payload: { cstId: record.cstid } });//查询货物存放地址列表
        // this.props.dispatch({ type: 'orderManage/queryBuyerList', payload: { cstId: record.cstid } });//查询境外买家
        // this.props.dispatch({ type: 'orderManage/querySupplyGoods', payload: { cstId: record.cstid } });//查询境内货源地
        this.refs.orderDetail.handleOpen(orderDisabled, letterDisabled, costDisabled, attachmentDisabled, searchConditions, tabActiveKey);
    }


    handleSearch = () => {
        let values = this.props.form.getFieldsValue();
        let newPage = this.props.orderManage.page;
        newPage.page = 1;
        this.props.dispatch({
            type: 'orderManage/queryOrderManageList',
            payload: {
                supplierId: this.props.supplierId,
                orderNo: values.orderNo,
                contractNo: values.contractNo,
                declarationType: values.declarationType,
                status: values.status,
                page: newPage
            }
        });

    };


    columns = [
        { title: '订单编号', dataIndex: 'orderno', key: 'orderno' },
        { title: '合同协议号', dataIndex: 'contractno', key: 'contractno' },
        { title: '进境关别', dataIndex: 'portofentryidName', key: 'portofentryidName' },
        { title: '总货值', dataIndex: 'totalamount', key: 'totalamount' },
        { title: '币种', dataIndex: 'currencytype', key: 'currencytype' },
        { title: '提交人', dataIndex: 'creatorName', key: 'creatorName' },
        { title: '提交时间', dataIndex: 'createtime', key: 'createtime', },
        { title: '预计报关时间', dataIndex: 'exporttime', key: 'exporttime' },
        {title: '操作', key: 'key_oprator',

            render: (text, record, index) => {
                return (
                    <span>
                        <a href="#" onClick={this.onView.bind(this, record)}>查看</a>
                    </span>

                );
            }
        }
    ];


    render() {
        const { getFieldDecorator } = this.props.form;
        let formItemLayout = {
            labelCol: { xs: { span: 24 }, sm: { span: 8 } },
            wrapperCol: { xs: { span: 24 }, sm: { span: 14 } }
        };
        let values = this.props.form.getFieldsValue();
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.orderManage.page.size,
            total: this.props.orderManage.page.totalRow,
            showSizeChanger: true,
            current: this.props.orderManage.page.page,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.orderManage.page;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'orderManage/queryOrderManageList',
                    payload: {
                        supplierId: this.props.supplierId,
                        orderNo: values.orderNo,
                        contractNo: values.contractNo,
                        declarationType: values.declarationType,
                        status: values.status,
                        page: newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.orderManage.page;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'orderManage/queryOrderManageList',
                    payload: {
                        supplierId: this.props.supplierId,
                        orderNo: values.orderNo,
                        contractNo: values.contractNo,
                        declarationType: values.declarationType,
                        status: values.status,
                        page: newPage
                    }
                });
            }
        }


        return (
            <div>
                <Form>
                    <Row>
                        <Col span={9}><FormItem {...formItemLayout} label={'订单编号'}>{getFieldDecorator('orderNo')(<Input />)}</FormItem></Col>
                        <Col span={9}><FormItem {...formItemLayout} label={'合同协议号'}>{getFieldDecorator('contractNo')(<Input />)}</FormItem></Col>
                    </Row>
                    <Row>

                        <Col span={9}><FormItem {...formItemLayout} label={'报关方式'}>
                            {getFieldDecorator('declarationType')(
                                <BizParams sprCode={"declarationtype"} style={{ width: '100%' }} all={true} />
                            )}
                        </FormItem></Col>
                        <Col span={9} >
                            <Button type="primary" icon="search" style={{marginLeft:20}} onClick={this.handleSearch.bind(this, null)}>查询</Button>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={7}>
                            <FormItem {...formItemLayout} label={'状态'}>
                                {getFieldDecorator('status', {
                                    initialValue: "1"
                                })(
                                    <RadioGroup style={{ width: 800 }} >
                                    <Radio value="1">草稿</Radio>
                                    <Radio value="2">待通关受理</Radio>
                                    <Radio value="3">待补充资料</Radio>
                                    <Radio value="5">待报关</Radio>
                                    <Radio value="6">报关中</Radio>
                                    <Radio value="7">办理完毕</Radio>
                                    </RadioGroup>
                                )}
                            </FormItem>
                        </Col>

                    </Row>
                </Form>
                <Table dataSource={this.props.orderManage.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} scroll={{ x: true }}/>

                <OrderDetail ref={"orderDetail"} {...this.props.customerManage} {...this.props.orderManage} dispatch={this.props.dispatch} customs={this.props.customs} />
            </div>
        )
    }

})


